export default class OrderBar {
    constructor(scene) {
        this.scene = scene;
        
        // 背景和容器
        this.paper = scene.add.image(540, 100, 'ui_order_paper')
          .setScrollFactor(0)
          .setDepth(10);
        
        this.ordersContainer = scene.add.container(540, 100)
          .setScrollFactor(0)
          .setDepth(11);
        
        // 状态显示
        this.coinText = scene.add.text(150, 50, '0', { 
            fontFamily: 'pixel_font', 
            fontSize: '32px',
            color: '#FFD700'
        }).setScrollFactor(0);
        
        this.weatherIcon = scene.add.image(900, 50, 'ui_weather_sunny')
          .setScrollFactor(0);
        
        // 订单闪烁动画
        this.tween = scene.tweens.addCounter({
            from: 0,
            to: 1,
            duration: 500,
            yoyo: true,
            repeat: -1
        });
        
        this.activeOrders = [];
    }

    updateOrder(orders) {
        // 添加空值检查
        if (!this.ordersContainer || !orders) return;
        
        this.ordersContainer.removeAll(true);
        this.activeOrders = orders;
        
        // 添加数组遍历保护
        orders?.forEach((order, i) => {
            const textureKey = `ui_order_${order.type}`;
            if (!this.scene.textures.exists(textureKey)) {
                console.error('缺少纹理:', textureKey);
                return;
            }
            
            const item = this.scene.add.sprite(i * 180, 0, textureKey)
                .setScale(0.8);
            
            if (!order.completed) {
                item.setTint(0xFF0000);
                item.setAlpha(this.tween.getValue());
            }
            
            this.ordersContainer.add(item);
        });
    }
    
    completeOrder(index) {
        if (this.activeOrders[index]) {
            // 完成特效
            const particles = this.scene.add.particles('ui_pixel_heart');
            particles.createEmitter({
                x: this.ordersContainer.list[index].x,
                y: 0,
                quantity: 10,
                lifespan: 1000,
                speed: { min: 100, max: 200 }
            });
            
            this.activeOrders[index].completed = true;
            this.updateOrder(this.activeOrders);
        }
    }
    
    setupGoldDisplay() {
        this.goldText = this.scene.add.text(100, 50, '0', { 
            fontSize: '32px',
            fontFamily: 'pixel_font'
        });
    }

    updateGoldText() {
        this.goldText.setText(this.scene.registry.values.gold);
    }

    generateRandomOrder() {
        const types = ['coffee', 'cake', 'tea'];
        const newOrder = {
            type: Phaser.Math.RND.pick(types),
            timeLimit: 30000, // 30秒时限
            reward: 100
        };
        
        this.activeOrders.push(newOrder);
        this.updateOrder(this.activeOrders);
        
        // 启动倒计时
        this.scene.time.delayedCall(newOrder.timeLimit, () => {
            if (!newOrder.completed) {
                this.failOrder(newOrder);
            }
        });
    }
}